package com.zhiche.lisa.bms.controller.invoice;


import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.zhiche.lisa.bms.controller.bill.BillController;
import com.zhiche.lisa.bms.dao.model.bill.Bill;
import com.zhiche.lisa.bms.dao.model.bill.BillIo;
import com.zhiche.lisa.bms.dao.model.fee.FeeAssessment;
import com.zhiche.lisa.bms.dao.model.interfacelog.InterfaceLog;
import com.zhiche.lisa.bms.dao.model.invoice.InvoiceApply;
import com.zhiche.lisa.bms.dao.model.invoice.InvoiceApplyAccy;
import com.zhiche.lisa.bms.dao.model.invoice.InvoiceReg;
import com.zhiche.lisa.bms.pojo.vo.common.CommonVO;
import com.zhiche.lisa.bms.pojo.vo.invoice.InvoiceApplyAndAccyRegVO;
import com.zhiche.lisa.bms.pojo.vo.invoice.InvoiceApplyAndAsseBillDetailVO;
import com.zhiche.lisa.bms.service.invoice.*;
import com.zhiche.lisa.bms.service.utils.InterfaceLogUtil;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 申请开票主表 前端控制器
 * </p>
 *
 * @author liuanshun
 * @since 2018-09-27
 */
@RestController
@RequestMapping("/invoiceApply")
@Api(value = "/invoiceApply", description = "申请开票管理接口", tags = {"申请开票管理接口"})
public class InvoiceApplyController {

    private static Logger LOGGER = LoggerFactory.getLogger(BillController.class);

    @Autowired
    private InvoiceApplyService invoiceApplyService;

    @Autowired
    private InvoiceApplyBillService invoiceApplyBillService;

    @Autowired
    private InvoiceApplyAccyService invoiceApplyAccyService;

    @Autowired
    private InvoiceApplyAsseService invoiceApplyAsseService;

    @Autowired
    private InvoiceRegService invoiceRegService;

    @Autowired
    private InterfaceLogUtil interfaceLogUtil;

    @PostMapping(value = "/selectInvoiceApplyPage")
    @ApiOperation(value = "获取开票主表分页信息", notes = "获取开票主表分页信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = InvoiceApply.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<InvoiceApply>> selectInvoiceApplyPage(
            @ApiParam(name = "分页查询对象", value = "传入json格式以及查询条件", required = true)
            @RequestBody Page<InvoiceApply> inParam) {
        LOGGER.info("/InvoiceApplyController.selectInvoiceApplyPage Param {}", inParam);
        RestfulResponse<Page<InvoiceApply>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(invoiceApplyService.selectInvoiceApplyPage(inParam));
        } catch (Exception e) {
            LOGGER.error("/InvoiceApplyController.selectInvoiceApplyPage ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/selectInvoiceApplyOne")
    @ApiOperation(value = "获取开票主表指定信息", notes = "获取开票主表指定信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = InvoiceApply.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<InvoiceApply> selectInvoiceApplyOne(
            @ApiParam(name = "分页查询对象", value = "传入json格式以及查询条件", required = true)
            @RequestBody InvoiceApply invoiceApply) {
        LOGGER.info("/InvoiceApplyController.selectInvoiceApplyOne Param {}", invoiceApply);
        RestfulResponse<InvoiceApply> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(invoiceApplyService.selectInvoiceApply(invoiceApply));
        } catch (BaseException e) {
            LOGGER.error("/InvoiceApplyController.selectInvoiceApplyOne ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/InvoiceApplyController.selectInvoiceApplyOne ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/selectInvoiceApplyAsseList")
    @ApiOperation(value = "获取申请考核开票详明列表信息", notes = "获取申请考核开票详明列表信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = FeeAssessment.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<FeeAssessment>> selectInvoiceApplyAsseList(
            @ApiParam(name = "开票绑定结果标识", value = "传入json格式以及查询条件", required = true)
            @RequestBody Map<String, String> inParam) {
        LOGGER.info("/InvoiceApplyController.selectInvoiceApplyAsseList Param {} ", inParam);
        RestfulResponse<List<FeeAssessment>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            String invoiceApplyId = inParam.get("invoiceApplyId");
            if (Strings.isNullOrEmpty(invoiceApplyId)) {
                throw new BaseException("请绑定申请开票的id");
            }
            restful.setData(invoiceApplyAsseService.selectInvoiceApplyAsseListByHeaderId(Long.valueOf(invoiceApplyId)));
        } catch (BaseException e) {
            LOGGER.error("/InvoiceApplyController.selectInvoiceApplyAsseList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/InvoiceApplyController.selectInvoiceApplyAsseList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/selectInvoiceApplyBillList")
    @ApiOperation(value = "获取申请账单开票详明列表信息", notes = "获取申请账单开票详明列表信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = Bill.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<BillIo>> selectInvoiceApplyBillList(
            @ApiParam(name = "开票绑定结果标识", value = "传入json格式以及查询条件", required = true)
            @RequestBody Map<String, String> inParam) {
        LOGGER.info("/InvoiceApplyController.selectInvoiceApplyAsseList Param {} ", inParam);
        RestfulResponse<List<BillIo>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            String invoiceApplyId = inParam.get("invoiceApplyId");
            if (Strings.isNullOrEmpty(invoiceApplyId)) throw new BaseException("请绑定申请开票的id");
            restful.setData(invoiceApplyBillService.selectInvoiceApplyBillListByHeaderId(Long.valueOf(invoiceApplyId)));
        } catch (BaseException e) {
            LOGGER.error("/InvoiceApplyController.selectInvoiceApplyBillList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/InvoiceApplyController.selectInvoiceApplyBillList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/insertInvoiceApplyAndDetail")
    @ApiOperation(value = "新增申请开票信息及详明", notes = "新增申请开票信息及详明", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> insertInvoiceApplyAndDetail(
            @ApiParam(name = "申请开票的头和详明信息", value = "传入json格式以及查询条件", required = true)
            @RequestBody InvoiceApplyAndAsseBillDetailVO invoiceApplyAndAsseBillDetailVO) {
        LOGGER.info("/InvoiceApplyController.insertInvoiceApplyAndDetail Param {} ", invoiceApplyAndAsseBillDetailVO);
        RestfulResponse<String> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        InterfaceLog interfaceLog = new InterfaceLog();
        try {
            interfaceLogUtil.setCalleeCommonLog(interfaceLog, invoiceApplyAndAsseBillDetailVO, "/invoiceApply/insertInvoiceApplyAndDetail");
            invoiceApplyService.insertInvoiceApplyAndDetail(invoiceApplyAndAsseBillDetailVO);
            interfaceLogUtil.setSuccessLog(interfaceLog);
        } catch (BaseException e) {
            LOGGER.error("InvoiceApplyController.insertInvoiceApplyAndDetail ERROR: {}", e.getMessage());
            interfaceLogUtil.setErrorLog(interfaceLog, e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("InvoiceApplyController.insertInvoiceApplyAndDetail ERROR: {}", e.getMessage());
            interfaceLogUtil.setErrorLog(interfaceLog, e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        // 保存日志
        interfaceLogUtil.saveInterfaceLog(interfaceLog);
        return restful;
    }

    @PostMapping(value = "/invoiceApplyExamine")
    @ApiOperation(value = "开票信息审核", notes = "开票信息审核", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<String>> invoiceApplyExamine(
            @RequestBody @ApiParam(name = "批量信息", value = "传入json格式以及查询条件", required = true) CommonVO commonVO) {
        LOGGER.info("InvoiceApplyController.invoiceApplyExamine Param {} ", commonVO);
        RestfulResponse<List<String>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            int i = 1;
            List<String> msg = new ArrayList<>();
            for (Long id :
                    commonVO.getIds()) {
                try {
                    invoiceApplyService.invoiceApplyExamine(id);
                    msg.add("开票信息:第" + id + "条:成功");
                } catch (BaseException e) {
                    restful.setCode(-2);
                    msg.add("开票信息:第" + id + "条:" + e.getMessage());
                } catch (Exception e) {
                    restful.setCode(-2);
                    msg.add("开票信息:第" + id + "条:" + "开票信息审核失败");
                }
                i++;
            }
            if (!CollectionUtils.isEmpty(msg))
                restful.setData(msg);
        } catch (BaseException e) {
            LOGGER.error("InvoiceApplyController.invoiceApplyExamine ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("InvoiceApplyController.invoiceApplyExamine ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/invoiceApplyDiscardedExamine")
    @ApiOperation(value = "开票信息弃审", notes = "开票信息弃审", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<String>> invoiceApplyDiscardedExamine(
            @ApiParam(name = "批量信息", value = "传入json格式以及查询条件", required = true)
            @RequestBody CommonVO commonVO) {
        LOGGER.info("/InvoiceApplyController.invoiceApplyExamine Param {} ", commonVO);
        RestfulResponse<List<String>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<String> msg = new ArrayList<>();
            int i = 1;
            for (Long id :
                    commonVO.getIds()) {
                try {
                    invoiceApplyService.invoiceApplyDiscardedExamine(id);
                    msg.add("开票信息:第" + i + "条:成功");
                } catch (BaseException e) {
                    restful.setCode(-2);
                    msg.add("开票信息:第" + i + "条:" + e.getMessage());
                } catch (Exception e) {
                    restful.setCode(-2);
                    msg.add("开票信息:第" + i + "条:" + "开票信息审核失败");
                }
                i++;
            }
            if (!CollectionUtils.isEmpty(msg))
                restful.setData(msg);
        } catch (BaseException e) {
            LOGGER.error("InvoiceApplyController.invoiceApplyDiscardedExamine ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("InvoiceApplyController.invoiceApplyDiscardedExamine ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/invoiceApplyUpdate")
    @ApiOperation(value = "开票信息修改", notes = "开票信息修改", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<String>> invoiceApplyUpdate(
            @ApiParam(name = "修改后的开票信息", value = "传入json格式以及查询条件", required = true)
            @RequestBody InvoiceApply invoiceApply) {
        LOGGER.info("/InvoiceApplyController.invoiceApplyUpdate Param {} ", invoiceApply);
        RestfulResponse<List<String>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            invoiceApplyService.updateInvoiceApplyAppointCloumn(invoiceApply);
        } catch (BaseException e) {
            LOGGER.error("InvoiceApplyController.invoiceApplyUpdate ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("InvoiceApplyController.invoiceApplyUpdate ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/selectInvoiceApplyRegList")
    @ApiOperation(value = "发票登记", notes = "发票登记", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = InvoiceApply.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<InvoiceReg>> selectInvoiceApplyRegList(
            @ApiParam(name = "对应主单信息", value = "传入json格式以及查询条件", required = true)
            @RequestBody InvoiceApply invoiceApply) {
        LOGGER.info("/InvoiceApplyController.selectInvoiceApplyRegList Param {} ", invoiceApply);
        RestfulResponse<List<InvoiceReg>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(invoiceRegService.selectInvoiceRegByHeaderId(invoiceApply.getId()));
        } catch (BaseException e) {
            LOGGER.error("InvoiceApplyController.selectInvoiceApplyRegList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("InvoiceApplyController.selectInvoiceApplyRegList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/insertOrUpdateInvoiceApplyRegList")
    @ApiOperation(value = "批量新增发票登记信息", notes = "批量新增发票登记信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = InvoiceApply.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<String>> insertOrUpdateInvoiceApplyRegList(
            @ApiParam(name = "分页查询对象", value = "传入json格式以及查询条件", required = true)
            @RequestBody CommonVO<InvoiceReg> invoiceApplyCommonVO) {
        LOGGER.info("/InvoiceApplyController.insertOrUpdateInvoiceApplyRegList Param {} ", invoiceApplyCommonVO);
        RestfulResponse<List<String>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            /*
             * 获取绑定数据
             */
            Long headerId;
            Object headerObj = invoiceApplyCommonVO.getCondition().get("headerId");
            if (headerObj == null || "".equals(headerObj)) {
                throw new BaseException("对应开票参数不存在");
            } else {
                headerId = Long.valueOf(headerObj.toString());
            }
            BigDecimal allMoney;
            BigDecimal itemAllMoney = new BigDecimal(0);
            Object allMoneyObj = invoiceApplyCommonVO.getCondition().get("allMoney");
            if (allMoneyObj == null || "".equals(allMoneyObj)) {
                throw new BaseException("对应开票总金额参数不存在");
            } else {
                allMoney = new BigDecimal(allMoneyObj.toString());
            }
            /*
             * 数据校验
             */
            for (InvoiceReg invoiceReg :
                    invoiceApplyCommonVO.getEntityParams()) {
                if (invoiceReg.getInvoiceSum() == null || BigDecimal.ZERO.compareTo(invoiceReg.getInvoiceSum()) == 0)
                    throw new BaseException("请设置发票价格");
                else {
                    itemAllMoney = itemAllMoney.add(invoiceReg.getInvoiceSum());
                }
            }
            if (allMoney.compareTo(itemAllMoney) != 0) throw new BaseException("发票总价格不符");
            /*
             * 保存处理
             */
            List<String> msg = new ArrayList<>();
            for (InvoiceReg invoiceReg :
                    invoiceApplyCommonVO.getEntityParams()) {
                try {
                    invoiceReg.setHeadId(headerId);
                    invoiceRegService.insertOrUpdateInvoiceReg(invoiceReg);
                } catch (BaseException e) {
                    msg.add("保存发票信息:" + ":" + e.getMessage());
                } catch (Exception e) {
                    msg.add("保存发票信息:" + ":保存异常");
                }
            }
            if (!CollectionUtils.isEmpty(msg))
                restful = new RestfulResponse(CommonEnum.ERRORLIST.getCode(), null, msg);
        } catch (BaseException e) {
            LOGGER.error("InvoiceApplyController.insertOrUpdateInvoiceApplyRegList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("InvoiceApplyController.insertOrUpdateInvoiceApplyRegList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/updateApplyAndRegDetail")
    @ApiOperation(value = "批量新增发票登记信息", notes = "批量新增发票登记信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = InvoiceApply.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<String>> updateInvoiceApplyAndDependentRegAndDependentAccy(@RequestBody InvoiceApplyAndAccyRegVO invoiceApplyCommonVO) {
        LOGGER.info("/InvoiceApplyController.updateInvoiceApplyAndDependentRegAndDependentAccy Param {} ", invoiceApplyCommonVO);
        RestfulResponse<List<String>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {

            InvoiceApply invoiceApply = new InvoiceApply();
            BeanUtils.copyProperties(invoiceApplyCommonVO, invoiceApply);
            invoiceApplyService.updateInvoiceApplyAppointCloumn(invoiceApply);

            BigDecimal itemAllMoney = new BigDecimal(0);
            BigDecimal allMoney = invoiceApply.getSumMoney();
            if (allMoney == null) {
                allMoney = BigDecimal.ZERO;
            }
            /*
             * 数据校验
             */
            for (InvoiceReg invoiceReg :
                    invoiceApplyCommonVO.getRegItems()) {
                invoiceReg.setHeadId(invoiceApply.getId());
                if (invoiceReg.getInvoiceSum() == null || BigDecimal.ZERO.compareTo(invoiceReg.getInvoiceSum()) == 0)
                    throw new BaseException("请设置发票价格");
                else {
                    itemAllMoney = itemAllMoney.add(invoiceReg.getInvoiceSum());
                }
            }
            // 2019-04-09 税率调整, 允许有差价存在
            // if (allMoney.compareTo(itemAllMoney) != 0) throw new BaseException("发票总价格不符");
            invoiceRegService.insertOrUpdateInvoiceRegList(invoiceApplyCommonVO.getRegItems(), invoiceApply.getId());
            invoiceApplyAccyService.editInvoiceApplyAccyListByHeadIdAndResource(invoiceApplyCommonVO.getAccyItems(), invoiceApply.getId());
        } catch (BaseException e) {
            LOGGER.error("InvoiceApplyController.updateInvoiceApplyAndDependentRegAndDependentAccy ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("InvoiceApplyController.updateInvoiceApplyAndDependentRegAndDependentAccy ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @GetMapping(value = "/getApplyAndRegDetail/{headId}")
    @ApiOperation(value = "获取发票登记信息", notes = "获取发票登记信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = InvoiceApply.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<InvoiceApplyAndAccyRegVO> getInvoiceApplyAndDependentRegAndDependentAccy(@PathVariable("headId") Long headId) {
        LOGGER.info("/InvoiceApplyController.getApplyAndRegDetail Param {} ", headId);
        RestfulResponse<InvoiceApplyAndAccyRegVO> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            InvoiceApplyAndAccyRegVO invoiceApplyAndAccyRegVO = new InvoiceApplyAndAccyRegVO();
            InvoiceApply invoiceApply = invoiceApplyService.selectById(headId);
            if (invoiceApply != null) {
                BeanUtils.copyProperties(invoiceApply, invoiceApplyAndAccyRegVO);
                invoiceApplyAndAccyRegVO.setAccyItems(invoiceApplyAccyService.selectInvoiceApplyAccyByHeadId(headId));
                invoiceApplyAndAccyRegVO.setRegItems(invoiceRegService.selectInvoiceRegByHeaderId(invoiceApply.getId()));
            }
            restful.setData(invoiceApplyAndAccyRegVO);
        } catch (BaseException e) {
            LOGGER.error("InvoiceApplyController.getApplyAndRegDetail ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("InvoiceApplyController.getApplyAndRegDetail ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/licenseConfim")
    @ApiOperation(value = "附件确认", notes = "附件确认", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> licenseConfim(
            @ApiParam(name = "分页查询对象", value = "传入json格式以及查询条件", required = true)
            @RequestBody InvoiceApply invoiceApply) {
        LOGGER.info("/InvoiceApplyController.licenseConfim Param {} ", invoiceApply);
        RestfulResponse<String> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            invoiceApplyService.invoiceApplyLicenseConfim(invoiceApply);
        } catch (BaseException e) {
            LOGGER.error("InvoiceApplyController.licenseConfim ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("InvoiceApplyController.licenseConfim ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @GetMapping(value = "/selectInvoiceApplyAccyByHeadId/{headId}")
    @ApiOperation(value = "根据头单id查询申请开票附件", notes = "根据头单id查询申请开票附件", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<InvoiceApplyAccy>> selectInvoiceApplyAccyByHeadId(@PathVariable("headId") Long headId) {
        LOGGER.info("/InvoiceApplyController.selectInvoiceApplyByHeadId Param {} ", headId);
        RestfulResponse<List<InvoiceApplyAccy>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(invoiceApplyAccyService.selectInvoiceApplyAccyByHeadId(headId));
        } catch (BaseException e) {
            LOGGER.error("InvoiceApplyController.selectInvoiceApplyByHeadId ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("InvoiceApplyController.selectInvoiceApplyByHeadId ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/insertInvoiceApplyAccyList")
    @ApiOperation(value = "批量新增申请开票附件", notes = "批量新增申请开票附件", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<InvoiceApplyAccy>> insertInvoiceApplyAccyList(@RequestBody CommonVO<InvoiceApplyAccy> invoiceApplyAccies) {
        LOGGER.info("/InvoiceApplyController.insertInvoiceApplyAccyList Param {} ", invoiceApplyAccies);
        RestfulResponse<List<InvoiceApplyAccy>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(invoiceApplyAccyService.insertInvoiceApplyAccyList(invoiceApplyAccies.getEntityParams(), Long.parseLong(invoiceApplyAccies.getTypeVal())));
        } catch (BaseException e) {
            LOGGER.error("InvoiceApplyController.insertInvoiceApplyAccyList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("InvoiceApplyController.insertInvoiceApplyAccyList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    @PostMapping(value = "/editInvoiceApplyAccyList")
    @ApiOperation(value = "编辑申请开票附件", notes = "编辑申请开票附件", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<InvoiceApplyAccy>> editInvoiceApplyAccyList(@RequestBody CommonVO<InvoiceApplyAccy> invoiceApplyAccies) {
        LOGGER.info("/InvoiceApplyController.editInvoiceApplyAccyList Param {} ", invoiceApplyAccies);
        RestfulResponse<List<InvoiceApplyAccy>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            restful.setData(invoiceApplyAccyService.editInvoiceApplyAccyListByHeadIdAndResource(invoiceApplyAccies.getEntityParams(), Long.parseLong(invoiceApplyAccies.getTypeVal())));
        } catch (BaseException e) {
            LOGGER.error("InvoiceApplyController.editInvoiceApplyAccyList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("InvoiceApplyController.editInvoiceApplyAccyList ERROR: {}", e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return restful;
    }

    /**
     * 取消开票通知
     *
     * @param invoiceApply
     * @return
     */
    @PostMapping(value = "/cancleInvoiceApply")
    @ApiOperation(value = "取消开票通知", notes = "取消开票通知", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<InvoiceApplyAccy>> cancleInvoiceApply(@RequestBody InvoiceApply invoiceApply) {
        LOGGER.info("/InvoiceApplyController.cancleInvoiceApply Param {} ", invoiceApply);
        RestfulResponse<List<InvoiceApplyAccy>> restful = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        InterfaceLog interfaceLog = new InterfaceLog();
        try {
            interfaceLogUtil.setCalleeCommonLog(interfaceLog, invoiceApply, "/invoiceApply/cancleInvoiceApply");
            invoiceApplyService.cancleInvoiceApplyForBms(invoiceApply);
            interfaceLogUtil.setSuccessLog(interfaceLog);
        } catch (BaseException e) {
            LOGGER.error("InvoiceApplyController.cancleInvoiceApply ERROR: {}", e.getMessage());
            interfaceLogUtil.setErrorLog(interfaceLog, e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("InvoiceApplyController.cancleInvoiceApply ERROR: {}", e.getMessage());
            interfaceLogUtil.setErrorLog(interfaceLog, e.getMessage());
            restful = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        // 保存日志
        interfaceLogUtil.saveInterfaceLog(interfaceLog);
        return restful;
    }

}

